<?php
/**
 * app端 订单管理
 * Author: lyun
 * Date: 2018/11/26
 * Created by Panxsoft.
 */
namespace App\Http\Controllers\Api\V1\App\Order;

use App\Exceptions\ApiErrCode;
use App\Exceptions\ApiException;
use App\Http\Controllers\ApiController;
use App\Http\Requests\Order\PayConfigIndexRequest;
use App\Http\Services\Api\V1\App\Product\OrderService;
use App\Models\Extension\Extension;
use App\Models\Order\Order;
use App\Models\Order\OrderHistory;
use App\Models\Setting;
use App\Services\Omnipay\AliPay;
use App\Services\Omnipay\AliPayGlobal;
use App\Services\Omnipay\Paypal;
use App\Services\Omnipay\WechatPay;
use App\Services\Orders\PayNotifyService;
use App\Services\Utils\Guard;
use App\Services\Utils\Transaction;

class PayConfigController extends ApiController
{
    /**
     * App端，订单支付的配置文件
     *
     * @param PayConfigIndexRequest $request
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function index(PayConfigIndexRequest $request)
    {
        /** @var Order $order */
        $order = $this->getOrder($request->order_id);

        // 验证订单是否已经支付了！
        $this->checkIsTrue(Order::validateOrderIsPayment($order), trans('order.already_paid'));

        // 验证订单是否过期了
        $this->checkIsTrue(Order::validateOrderIsExpire($order), trans('order.past_due'));

        // 修改订单支付方式
        if ($request->payment_code) {
            (new OrderService)->patchOrderPayment($order, $request->payment_code);
        }

        // 验证订单是否有支付方式
        $this->checkIsTrue($order->payment_code, trans('order.payment_code_required'));

        // 生成App支付的订单配置信息
        $config = $this->generatePayConfig($order);

        return $this->response(compact('config'));
    }

    /**
     * @param $order_id
     * @return Order
     * @throws \App\Exceptions\ApiAbstractException
     */
    private function getOrder($order_id)
    {
        /** @var Order $order */
        $order = (new Order)->with('orderTotalByTotal')->find($order_id);

        $this->checkItemExist($order, 'order');
        $this->checkIsTrue($order->customer_id === Guard::customer()->getKey(), trans('order.customer_not_match'));

        return $order;
    }

    /**
     * @param Order $order
     *
     * @return mixed
     * @throws \Exception
     */
    private function generatePayConfig(Order $order)
    {
        switch ($order->payment_code) {
            case Extension::CODE_PAYMENT_WECHAT:
                return WechatPay::getConfig($order);

            case Extension::CODE_PAYMENT_ALI:
                return AliPay::getConfig($order);

            // RSA验签失败，可能要单独申请国际支付宝。
//            case Extension::CODE_PAYMENT_ALI_GLOBAL:
//                return AliPayGlobal::getConfig($order);

            case  Extension::CODE_PAYMENT_PAYPAL:
                return (new PayPal)->payPalApp($order);

            case  Extension::CODE_PAYMENT_COD:
                return $this->changeOrderStatus($order);

            default:
                throw new ApiException(ApiErrCode::CUSTOMIZE_ERROR, 'not support pay extension');
                break;
        }
    }

    /**
     * @param Order $order
     * @return mixed
     * @throws \Exception
     */
    private function changeOrderStatus(Order $order)
    {
        (new PayNotifyService)->savePaidStatus($order);

        return true;
    }
}
